package com.dhecp.project.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.flowable.mapper.SysProcessInstanceMapper;
import com.dhecp.project.flowable.service.ActProcessService;
import com.dhecp.project.system.domain.taskedit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.project.system.domain.SysUserInfoSimple;
import com.dhecp.project.system.service.SysUserModuleAgentService;

/**
 * 用户功能代理 控制层
 * @author lwl
 * @version 2019年10月31日
 */
@Controller
@RequestMapping("/system/agent")
public class SysUserModuleAgentController extends BaseController {

	private String prefix = "system/agent";
    
    @Autowired
    private SysUserModuleAgentService userModuleAgentService;

    @Autowired
    private ActProcessService actProcessService;

    @Autowired
    private SysProcessInstanceMapper processInstanceMapper;

    /**
     * 查询指定用户可代理的用户列表
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
    @GetMapping("/mandatorlist")
    @ResponseBody
    public TableDataInfo list(@RequestParam String userName){
        List<SysUserInfoSimple> list = new ArrayList<SysUserInfoSimple>();
        try {
            list = userModuleAgentService.queryUserAgentMandator(userName);
        } catch (Exception e) {
            return getDataTable(this.getExceptionMessage(e).toString());            
        }
        return getDataTable(list);
    }


    /**
     * 处理无效任务
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
    @PostMapping("/taskDispose")
    @ResponseBody
    public AjaxResult taskDispose(@RequestParam Map<String, Object> params) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<taskedit> items = JSON.parseArray(params.get("taskItems").toString(), taskedit.class);

            for (taskedit item: items
            ) {
                String procInstId = item.getProcInstId();
                if (procInstId != null && !"".equals(procInstId)) {
                    actProcessService.deleteProcess(procInstId);
                    //插入指定单据流转各环节审批人记录（单据删除记录）
                    Map<String, Object> flowRecord = new HashMap<String, Object>();
                    flowRecord.put("moduleTable", "");
                    flowRecord.put("nid", item.getNid());
                    flowRecord.put("activeFlowOld", "FlowZd");
                    flowRecord.put("procInstId", procInstId);
                    flowRecord.put("createUser", item.getOuser());
                    flowRecord.put("createUserName", item.getName());
                    flowRecord.put("updateUsers", "实际删除操作人： " + item.getName() + "【" + item.getOuser() + "】");
                    flowRecord.put("optType", "delete");
                    processInstanceMapper.insertProcessInstanceFlowRecord(flowRecord);
                }
            }

        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    //旧版本邮件导入新邮件
    
}
